<?php

namespace App\Http\Controllers;

use App\fish_kind;
use App\fish_price;
use App\Jobs\SendEmail;
use DemeterChain\C;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\Request;
use Illuminate\Mail\Mailer;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
use Illuminate\Contracts\Cache\Repository as Cache1;
use Illuminate\Support\Facades\Storage;

class StudentController extends Controller
{
    protected $cookie_path;
    public function __construct()
    {
        $this->cookie_path=storage_path().'/cookie.txt';
    }

    public function upload(Request $request)
    {
        if ($request->isMethod('post')){
            $file=$request->file('source');

            //文件是否成功

            if ($file->isValid()){
               $originalName= $file->getClientOriginalName();
                $ext=$file->getClientOriginalExtension();
                $type=$file->getMimeType();
                $realPath=$file->getRealPath();
                $filename=date('Y-m-d-H-i-s').'-'.uniqid().'.'.$ext;
                //第一种保存方法
                $path=$file->storeAs('uploads',$filename );
                
//                var_dump($path);exit;
                //第二种保存方法
//                $bool=Storage::disk('uploads')->put($filename,file_get_contents($realPath));
                $destinationPath = 'uploads';
                $file->move($destinationPath,$filename);

            }
            var_dump(storage_path());exit;


        }
        return view('students.upload');

    }
    public function mail(){
        Mail::raw('逗比,少打游戏多动脑,免得老年痴呆',function ($message){
           $message->from('syj198920@163.com','今晚打老虎');
            $message->subject('老虎不发威,你当哥是hellokitty');
            $message->to('443736510@qq.com');
        });
//        Mail::send('students.mail',['name'=>'你是猴子请来的逗比么??嘿嘿哈嘿'],function ($message){
//            $message->from('syj198920@163.com','今晚打老虎');
//            $message->to('865480936@qq.com');
//
//        });

    }
    //缓存
    public function cache1()
    {
        Cache::put('syj',12345,11);


    }
    //缓存2
    public function cache2(){
        $val=Cache::get('syj');
        var_dump($val);

    }
    public function error()
    {
//        Log::info('info日志');

//        Log::warning('1111111111111111111');
        Log::debug('这是一个数组',['name'=>'syj3','age'=>18]);
//        abort('404','故障啦');
//        return view('students.upload1');

    }
    public function queue(){
        $this->dispatch(new SendEmail(['name'=>'syj2','age'=>10]));
    }
    public function test1(Cache1 $cache){


    }
    public function getMessage(){

        $cookie_file = storage_path().'/cookie.txt';
//        $url = "http://help.cninfoyu.cn/Account/Login";
        $url = "http://help.cninfoyu.cn/Account/CheckCode?ID=1";
        $ch = curl_init($url); //初始化
        curl_setopt($ch, CURLOPT_HEADER, 0); //不返回header部分
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //返回字符串，而非直接输出
        curl_setopt($ch, CURLOPT_COOKIEJAR,  $cookie_file); //存储cookies
        $a=curl_exec($ch);
        curl_close($ch);





    }
    function curl_post_https($url,$data,$auth){

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER , true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type:application/json',
                'authorization:'.$auth)
        );

        $tmpInfo = curl_exec($ch); // 执行操作
        if (curl_errno($ch)) {
            echo 'Errno'.curl_error($ch);//捕抓异常
        }
        curl_close($ch); // 关闭CURL会话
        return $tmpInfo; // 返回数据，json格式
    }
    public function showAuthcode( $authcode_url )
    {
        $cookieFile = SCRIPT_ROOT.'cookie.tmp';
        $ch = curl_init($authcode_url);
        curl_setopt($ch,CURLOPT_COOKIEJAR,$cookieFile  ); // 把返回来的cookie信息保存在文件中
        curl_exec($ch);
        curl_close($ch);
    }
    //插入鱼种类
    public function fishKind(){
        $targetUrl = 'http://help.cninfoyu.cn/SysSetting/GetProductNode';
//        $targetUrl = 'http://help.cninfoyu.cn/DataMining/GetPriceNow?pid=6&ptype=2&rid=0&rtype=1&PageSize=20';
        $curlPost=[
            'id'=>'0','type'=>'1','value'=>'0', 'rid'=>'-1',
        ];
        $kind_data=[];
        for ($i=0;$i<=8;$i++){
            $kind_data_tmp=[];
            $re= $this->curl_post($targetUrl,$curlPost);
            $curlPost['id']++;
            $curlPost['value']++;
            $curlPost['type']='2';
            $kind_data_tmp=json_decode($re,true);
            $kind_data= array_merge($kind_data,$kind_data_tmp);
        }
        foreach ($kind_data as $key =>$fish_kind){
            if ($fish_kind['isParent']===true){
                $kind_data[$key]['isParent']=1;
            }else{
                $kind_data[$key]['isParent']=0;
            }
        }
        $fish=new fish_kind();
        $fish->where('id','>','-1')->delete();
        $fish->addAll($kind_data);
        echo 'success';


//        $targetUrl = 'http://help.cninfoyu.cn/DataMining/GetPriceNow?pid=6&ptype=2&rid=0&rtype=1&PageSize=20';

//        echo $re ;

    }
    //获取缓存
    public function setTmp(){
        header('Content-Type:image/png');
        $authcode_url='http://help.cninfoyu.cn/Account/CheckCode?ID=1';
        $cookieFile = storage_path().'/cookie.txt';
        $ch = curl_init($authcode_url);
        curl_setopt($ch,CURLOPT_COOKIEJAR, $cookieFile); // 把返回来的cookie信息保存在文件中
        curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
        $re=curl_exec($ch);
        curl_close($ch);
        echo  'success';
    }
    //发送curlget请求
    public function  curl_get($targetUrl){
        $cookieFile = $this->cookie_path;
        $ch = curl_init($targetUrl);
        curl_setopt($ch,CURLOPT_COOKIEFILE, $cookieFile); //同时发送Cookie
        curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
        $re= curl_exec($ch);
        curl_close($ch);
        return $re;

    }
    //发送curlpost请求
    public function  curl_post($targetUrl,$curlPost){
        $cookieFile = $this->cookie_path;
        $ch = curl_init($targetUrl);
        curl_setopt($ch,CURLOPT_COOKIEFILE, $cookieFile); //同时发送Cookie
        curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch,CURLOPT_POST,1); //设置请求发送方式，post或get，CURLOPT_POST或CURLOPT_GET
        curl_setopt($ch,CURLOPT_POSTFIELDS,$curlPost);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
//                'Content-Type:application/json',
                'Host:help.cninfoyu.cn'
            )
        );
        $re= curl_exec($ch);
        curl_close($ch);
        return $re;

    }
    public function  yzm(){
        header('Content-Type:image/png');
        $cookie_file = storage_path().'/cookie.txt';
        $url = "http://help.cninfoyu.cn/Account/CheckCode?ID=1";
        $ch = curl_init($url); //初始化
        curl_setopt($ch, CURLOPT_COOKIEJAR,  $cookie_file); //存储cookies
        curl_exec($ch);
        curl_close($ch);exit;
    }
    //首页信息
    public function index(Request $request){
        if ($request->isMethod('POST')){
            $loginParams['password'] = 'csfish5919';
            $loginParams['username'] = 'admin';
            $loginParams['code'] = $request->input('code');
            $loginParams['x'] = 11;
            $loginParams['y'] = 22;
            // $cookieFile 为加载验证码时保存的cookie文件名
            $cookieFile = $this->cookie_path;
            // $targetUrl curl 提交的目标地址
            $targetUrl = 'http://help.cninfoyu.cn/Account/Logined';
            // 登陆
            $content = $this->curlLogin($targetUrl, $cookieFile, $loginParams);
           return view('students.list');
        }
        return view('students.index',['message'=>false]);
    }
   public function curlLogin($url, $cookieFile, $loginParams)
    {
        $ch = curl_init($url);
        curl_setopt($ch,CURLOPT_COOKIEFILE, $cookieFile); //同时发送Cookie
        curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch,CURLOPT_POST, 1);
        curl_setopt($ch,CURLOPT_POSTFIELDS, $loginParams); //提交查询信息
        $content = curl_exec($ch);
        curl_close($ch);
        return $content;
    }
    //鱼批发价行情
    public function  fishPrice(){
        $targetUrl =  'http://help.cninfoyu.cn/DataMining/GetPriceNow?pid=0&ptype=1&rid=0&rtype=1&PageSize=2000';
        $data=$this->curl_get($targetUrl);
        $data=json_decode($data,true);
        $price_data=[];
//        "uid":446,
//            "psid":1133,
//            "date":"2018-06-11",
//            "product_name":"青鱼",
//            "standard_name":"条重＜2000",
//            "unit_name":"江西南昌市水产品综合交易批发市场",
//            "highest_price":18,
//            "central_parity":16,
//            "lowest_price":14,
//            "price_notes":""
        foreach ($data['Items'] as $key =>$value){
            $price_data[]=[
                'fish_kind'=>11,
                'fish_size'=>$value['psid'],
                'fish_name'=>$value['product_name'],
                'fish_size_name'=>$value['standard_name'],
                'min_price'=>$value['lowest_price'],
                'avg_price'=>$value['central_parity'],
                'max_price'=>$value['highest_price'],
                'source'=>$value['uid'],
                'source_name'=>$value['unit_name'],
                'created'=>strtotime($value['date']),
                'updated'=>time(),
            ];
        }
        $fish_price=new  fish_price();
        $fish_price->addAll($price_data);
        echo 'success';

    }
    //鱼规格
    public function  fishCate(){
        $targetUrl = 'http://help.cninfoyu.cn/SysSetting/GetProductNode';
        $data=fish_kind::where('type','3')->get();
        $fish_cate=[];
        $i=1;
        foreach ($data as $a){

            $curlPost=[
                'id'=>  $a->id,
                'value'=>$a->value,
                'rid'=> -1,
                'type'=> 3,
            ];
            $fish_cate_tmp=$this->curl_post($targetUrl,$curlPost);
            $fish_cate_tmp=(json_decode($fish_cate_tmp,true));
            if ($fish_cate_tmp&&is_array($fish_cate_tmp)){
                $fish_cate=array_merge($fish_cate,$fish_cate_tmp);
            }
            foreach ($fish_cate as $key =>$value){
               if ( $fish_cate[$key]['isParent']==false){
                   $fish_cate[$key]['isParent']=0;
               }
            }
            $fish=new  fish_kind();
            $fish->where('type',4)->delete();
            $fish->addAll($fish_cate);



        }

    }

}
